package com.diyizu.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.diyizu.youdianda.common.R;
import com.diyizu.youdianda.dto.YddLinkDto;
import com.diyizu.youdianda.entity.YddLink;
import com.diyizu.youdianda.service.YddLinkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 友情链接
 */
@Slf4j
@RestController
@RequestMapping("/ydd_link")//advertisement
public class AdvertisementController {

    @Autowired
    private YddLinkService yddLinkService;

    //文字/图片链接列表
    @GetMapping("/list")//friendlink
    public R<YddLinkDto> getall(Integer type, Integer page, Integer limit) {
        //YddLink byId = yddLinkService.getById(id);
        log.info("友情链接管理列表展示：type="+type+";page="+page+";limit="+limit);
        YddLinkDto yddLinkDto = yddLinkService.getAll(type, page, limit);
        return R.success(yddLinkDto);
    }

    //链接详情(回显数据)
    @GetMapping("/rowInfo")
    public R<YddLink> rowInfo(Integer id){
        log.info("链接详情：id="+id);
        YddLink byId = yddLinkService.getById(id);
        return R.success(byId);
    }

    //删除链接
    @GetMapping("/del")
    public R<String> del(Integer id) {
        boolean b = yddLinkService.removeById(id);
        if (b) {
            return R.success("删除成功");
        } else
            return R.error("删除失败");
    }

    //修改链接
    @PostMapping("/update")
    public R<String> update(@RequestBody YddLink link) {
        LambdaQueryWrapper<YddLink> linkLambdaQueryWrapper = new LambdaQueryWrapper<>();
        boolean b = yddLinkService.updateById(link);
        if (b) {
            return R.success("更新成功");
        } else {
            return R.error("更新失败");
        }
    }

    //添加链接
    @PostMapping("/add")
    public R<String> add(@RequestBody YddLink link) {
        boolean save = yddLinkService.save(link);
        if (save) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }
}
